﻿using Device.Domain;
using Device.Read.API.Applcation.Command.EquipmentCommand;
using Device.Read.API.Dtos;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace Device.Read.API.Controllers
{
    /// <summary>
    /// 设备相关接口(读)
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class EquipmentController : ControllerBase
    {


        private readonly IMediator _mediator;

        public EquipmentController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 获取设备列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<EquipmentModel>>> GetEquipmentList([FromQuery] SelectEquipmentCommand command)
        {
           ApiResult<PageDTO<EquipmentModel>> result = new ApiResult<PageDTO<EquipmentModel>>();    

            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.Uid = int.Parse(uid);
                command.Oid = sid;
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<EquipmentModel>> FileEquipmentInfo([FromQuery]FillEquipmentCommand command)
        {
              ApiResult<EquipmentModel> result = new ApiResult<EquipmentModel>();
            try
              {
                  result.Data = await _mediator.Send(command);
              }
              catch (Exception ex)
              {
                  result.Code = 500;
                  result.Message = ex.Message;
              }

              return result;
        }



        /// <summary>
        /// //导出excel
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ExportExcelStore([FromQuery] SelectEquipmentCommand command)
        {

            var list = await _mediator.Send(command);

            IWorkbook wb = new XSSFWorkbook();
            ISheet sheet = wb.CreateSheet("设备信息");

            IRow row = sheet.CreateRow(0);
            row.CreateCell(0).SetCellValue("设备编号");
            row.CreateCell(1).SetCellValue("设备名称");
            row.CreateCell(2).SetCellValue("授权标识符");
            row.CreateCell(3).SetCellValue("状态");
            row.CreateCell(4).SetCellValue("备注");
            row.CreateCell(5).SetCellValue("创建人");
            row.CreateCell(6).SetCellValue("创建时间");

            int i = 1;
            foreach (var item in list.list)
            {
                row = sheet.CreateRow(i);
                row.CreateCell(0).SetCellValue(item.EquipmentId);
                row.CreateCell(1).SetCellValue(item.EquipmentName);
                row.CreateCell(2).SetCellValue(item.AuthorIdentifier);
                row.CreateCell(3).SetCellValue(item.EquipmentState==1?"闲置":"启用");
                row.CreateCell(4).SetCellValue(item.Eremark);
                row.CreateCell(5).SetCellValue(item.AddName);
                row.CreateCell(6).SetCellValue(item.AddTime.ToString());
                i++;
            }

            byte[] bytes;


            using (MemoryStream ms = new MemoryStream())
            {
                wb.Write(ms);
                bytes = ms.ToArray();
            }

            string mapPath = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(bytes, mapPath, "设备信息.xlsx");

        }





    }
}
